//
//  UserInfo.m
//  Core
//
//  Created by Cuong Doan on 3/4/13.
//  Copyright (c) 2013 Cuong Doan. All rights reserved.
//

#import "UserInfo.h"

@interface UserInfo ()

//
// get user info with specific user id
// @param: userID
//
-(void)getUserByID:(int)userID;

@end

@implementation UserInfo
{
    sqlite3 *db;    
}

@synthesize USERID, UserName, Ready, Activated;

-(id)initWithID:(int)userID
{
    self = [super init];
    
    @try
    {
        [self getUserByID:userID];
    }
    @catch (NSException *exception)
    {
        @throw exception;
    }  
    
    return self;
}

-(void)getUserByID:(int)userID
{
    sqlite3_stmt *statement;
    
    const char *sql = "Select * from UserInfo where User_ID = (?)";   
    
    @try
    {
        [DatabaseManager openAndPrepareDatabase:&db andStatement:&statement andSQL:sql];
        
        [DatabaseManager bindInt:statement andValue:userID andIndex:1];
        
        while(sqlite3_step(statement) == SQLITE_ROW)
        {
            self.USERID = sqlite3_column_int(statement, 0);
            
            self.UserName = [NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 1)];
            
            self.Ready = (bool)sqlite3_column_int(statement, 2);
            
            self.Activated = (bool)sqlite3_column_int(statement, 3);
        }
    }
    @catch (NSException *exception)
    {
        @throw exception;
    }
    @finally
    {
        sqlite3_close(db);
    }
}

-(void)updateUser
{
    sqlite3_stmt *statement;
    
    const char *sql = "Update UserInfo set User_Name = ?, Ready = ?, Activated = ? where User_ID = ?";       
        
    @try
    {
        [DatabaseManager openAndPrepareDatabaseV2:&db andStatement:&statement andSQL:sql];
        
        [DatabaseManager bindText:statement andValue:self.UserName andIndex:1];
        
        [DatabaseManager bindInt:statement andValue:(int)self.Ready andIndex:2];
        
        [DatabaseManager bindInt:statement andValue:(int)self.Activated andIndex:3];
        
        [DatabaseManager bindInt:statement andValue:self.USERID andIndex:4];
        
        sqlite3_step(statement);
        
        sqlite3_finalize(statement);
    }
    @catch (NSException *exception)
    {
        @throw exception;
    }
    @finally
    {
        sqlite3_close(db);
    }
}

-(void)insertUser
{
    sqlite3_stmt *statement;
    
    const char *sql = "insert into UserInfo (UserName, Ready, Activated) values (?, ?, ?)";            
        
    @try
    {
        [DatabaseManager openAndPrepareDatabaseV2:&db andStatement:&statement andSQL:sql];
        
        [DatabaseManager bindText:statement andValue:self.UserName andIndex:1];
        
        [DatabaseManager bindInt:statement andValue:self.Ready andIndex:2];
        
        [DatabaseManager bindInt:statement andValue:self.Activated andIndex:3];
        
        sqlite3_step(statement);
        
        sqlite3_finalize(statement);

    }
    @catch (NSException *exception)
    {
        @throw exception;
    }
    @finally
    {
        sqlite3_close(db);
    }    
}

+(int)getActivatedUserID
{
    sqlite3 *db;
    
    int userID = 0;
    
    sqlite3_stmt *statement;
    
    const char *sql = "select User_ID from UserInfo where Activated = ?";       
        
    @try
    {
        [DatabaseManager openAndPrepareDatabase:&db andStatement:&statement andSQL:sql];
        
        [DatabaseManager bindInt:statement andValue:1 andIndex:1];
                
        while(sqlite3_step(statement) == SQLITE_ROW)
        {
            userID = sqlite3_column_int(statement, 0);
        }
        
        return userID;
    }
    @catch (NSException *exception)
    {
        @throw exception;
    }
    @finally
    {
        sqlite3_close(db);
    }
}

@end
